package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by Administrator on 2019/5/26 026.
 */
public class Submit
{
    public static void main(String[] args)
    {
        ExecutorService ss = Executors.newSingleThreadExecutor();
        List<Future<String>> aa = new ArrayList<>();
        new Thread(() ->
        {
            try
            {
                Thread.sleep(2000);
                for(Future<String> d : aa)
                {
                    d.cancel(false);
                }
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }).start();
        for (int i = 0; i < 5; i++)
        {
            Future<String> fs = ss.submit(new DD("abc" + i));
            aa.add(fs);
        }

        for(Future<String> s : aa)
        {
            try
            {
                System.out.println(s.get());
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            } catch (ExecutionException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static String bo(String s)
    {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length(); i++)
        {
            char aa = s.charAt(i);
            sb.append(Integer.toHexString(aa) + " ");
        }
        return sb.toString();
    }

    static class DD implements Callable<String>
    {

        String s;
        public DD(String x)
        {
            s = x;
        }

        @Override
        public String call() throws Exception
        {
            System.out.println("running " + s);
            Thread.sleep(3000);
            return s;
        }
    }
}
